/**
 * 
 */
package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.igas.bomp.facade.BompTransMonitorService;
import cc.rengu.igas.bomp.facade.request.KeySynchroniseRequest;
import cc.rengu.igas.bomp.facade.response.KeySynchroniseResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.service.RealTaskService;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.RedpResponse;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RealTaskServiceImpl implements RealTaskService {

    private final static Logger logger = LoggerFactory.getLogger(RealTaskServiceImpl.class);

    @Override
	public RedpResponse keySynchronise(String instId, KeySynchroniseRequest req, BompTransMonitorService bompTransMonitorService, String traceNo){
    	RedpResponse redpResponse = new RedpResponse();
		req.setHeader(CommonUtils.packageBompHeader(traceNo,BompTxnNumEnum.keySynchronise.getType(),instId,null));

		java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
		try{

			logger.info("法人行["+instId+"],用户标识[" + req.getUserId() + "]"+BompTxnNumEnum.keySynchronise.getDesc()+"交易请求报文：" + JSON.toJSONString(req));

			Result<KeySynchroniseResponse> response= bompTransMonitorService.keySynchronise(req);

			logger.info("法人行["+instId+"],用户标识[" + req.getUserId() + "]"+BompTxnNumEnum.keySynchronise.getDesc()+"交易返回报文：" + JSON.toJSONString(response));
			if(response.isSuccess()){
				KeySynchroniseResponse resp = response.getResult();
				// 返回码：00-进件成功，其他均视为失败
				if (BusinessContainer.BSPS_SUCCESS.equals(resp.getRespCode())) {
					result.put("respCode", resp.getRespCode());
					result.put("respMsg", resp.getRespDesc());
					logger.error("法人行["+instId+"],用户标识[" + req.getUserId() + "]"+BompTxnNumEnum.keySynchronise.getDesc()+"交易成功");
					return redpResponse.message(BompTxnNumEnum.keySynchronise.getDesc()+"交易成功"+result.toString()).success();
				}else {
					result.put("respCode", resp.getRespCode());
					result.put("respMsg", resp.getRespDesc());
					return redpResponse.message(BompTxnNumEnum.keySynchronise.getDesc()+"交易失败"+result.toString()).failure();
				}
			}else{
				result.put("resCode", BusinessContainer.ERROR);
				result.put("resMsg", BompTxnNumEnum.mchntImageInfoManage.getDesc()+"没有获得返回报文");
				logger.error("法人行["+instId+"],用户标识[" + req.getUserId() + "]"+BompTxnNumEnum.keySynchronise.getDesc()+"交易没有获得返回信息");
				return redpResponse.message(BompTxnNumEnum.keySynchronise.getDesc()+"交易失败"+result.toString());
			}
		}catch (Exception e) {
			// TODO: handle exception
			result.put("resCode", BusinessContainer.ERROR);
			result.put("resMsg", e.getMessage());
			logger.error("法人行["+instId+"],用户标识[" + req.getUserId() + "]"+BompTxnNumEnum.keySynchronise.getDesc()+"交易出现异常，异常信息："+e.getMessage(),e);
			return redpResponse.message(BompTxnNumEnum.keySynchronise.getDesc()+"交易失败"+result.toString());
		}
	}

}
